﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace Wolfut.MirageEE.UI.UIBaseComponents
{
    public partial class UserControlUploadImage:MirageEEUserControlBase
    {
        public UserControlUploadImage()
        {
            InitializeComponent();
        }
        public Image UploadImage
        {
            get;
            set;
        }
        protected override void OnLoad(EventArgs e)
        {
            //base.OnLoad(e);
            this.simpleButtonFileUpload.Click += new EventHandler(FileUploadClick);
            this.openFileDialogUploadImage.InitialDirectory = this.GetType().Assembly.Location.Substring(0, this.GetType().Assembly.Location.LastIndexOf(@"\"));
            openFileDialogUploadImage.FileName = String.Empty;
            openFileDialogUploadImage.Filter = "(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF;";
        }
        private void FileUploadClick(Object sender, EventArgs e)
        {
            openFileDialogUploadImage.ShowDialog();
            if (!String.IsNullOrEmpty(openFileDialogUploadImage.FileName))
            {
                textEditFilePath.Text = openFileDialogUploadImage.FileName;
                UploadImage = Image.FromFile(openFileDialogUploadImage.FileName);
            }
            this.OnImageUploadClick(e);
        }

        #region Events
        private static Object objImageClick = new Object();
        public event EventHandler ImageUploadClick
        {
            add
            {
                lock (objImageClick)
                {
                    base.Events.AddHandler(objImageClick, value);
                }
            }
            remove
            {
                lock (objImageClick)
                {
                    base.Events.RemoveHandler(objImageClick, value);
                }
            }
        }
        protected virtual void OnImageUploadClick(EventArgs e)
        {
            EventHandler handler = (EventHandler)base.Events[objImageClick];
            if (handler != null)
            {
                handler(this, e);
            }
        }
        #endregion

        /// <summary>
        /// 默认保存到程序目录下,同时返回image的名字
        /// </summary>
        public String SaveImage()
        {
            if (this.UploadImage != null)
            {
                String pathName = this.GetType().Assembly.Location.Substring(0, GetType().Assembly.Location.LastIndexOf(@"\"));  
                String subPath=String.Format(@"\UploadImage\{0}",DateTime.Now.ToString("yyyyMMdd"));
                pathName += subPath;
                if (!Directory.Exists(pathName))
                {
                    Directory.CreateDirectory(pathName);
                }
                String imgName = String.Format("{0}.jpg", Guid.NewGuid().ToString().Replace("-",String.Empty));
                File.Copy(openFileDialogUploadImage.FileName,String.Format(@"{0}\{1}",pathName,imgName));
                return String.Format(@"{0}\{1}", subPath,imgName);
            }
            return String.Empty;
        }
        /// <summary>
        /// 获取选择图片的byte[];
        /// </summary>
        /// <returns></returns>
        public byte[] GetImageByteArray()
        {
            if(this.UploadImage!=null)
            {
                Stream stream=openFileDialogUploadImage.OpenFile();
                byte[] outBytes=new Byte[stream.Length];
                stream.Position = 0;
                stream.Read(outBytes, 0, outBytes.Length);
                stream.Close();
                return outBytes ;
            }
            return null;
        }
        /// <summary>
        /// 生成特定大小的预览图
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public Image GenerateImage(int width, int height)
        {
            if (this.UploadImage != null)
            {
                Image img = this.UploadImage;
                Image desImage = new Bitmap(width, height);
                Graphics g = Graphics.FromImage(desImage);
                g.DrawImage(UploadImage,
                            new Rectangle(0, 0, width, height),
                            new Rectangle(0, 0, img.Width, img.Height),
                            GraphicsUnit.Pixel);
                return desImage;
            }
            return null;
        }
    }
}
